﻿using System;
using System.Linq;
using System.Web.Mvc;
using VedranKrizicPortfolio.Models;
using System.Collections.Generic;

namespace VedranKrizicPortfolio.Controllers
{
   
    [Authorize]
    public class AdminController : Controller
    {
        vedranportfolioEntities vk = new vedranportfolioEntities();

        //
        // GET: /Admin/
        public ActionResult Index()
		{
			if (Request.IsAjaxRequest())
			{
				return PartialView();
			}
			else
			{
				return View();
			}
		}

        // GET: /Admin/ArticleList

        public ActionResult ArticleList()
        {
            var model = vk.Article.OrderByDescending(a => a.DateAdded);
			if (Request.IsAjaxRequest())
			{
				return PartialView(model.ToList());
			}
			else
			{
				return View(model.ToList());
			}
           
        }

        public ActionResult ArticleEdit(int id)
        {
            IEnumerable<ArticleType> listAT = vk.ArticleType;
            ViewBag.ArticleTypes = listAT;
            return View(vk.Article.Where(a => a.id == id).FirstOrDefault());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ArticleEdit(Article article)
        {
            if (ModelState.IsValid)
            {
                vk.Article.Attach(article);
                vk.ObjectStateManager.ChangeObjectState(article, System.Data.EntityState.Modified);
                vk.SaveChanges();
                return RedirectToAction("ArticleList");
            }
            else
            {
                return View(article);
            }

        }

        [ValidateInput(false)]
        public ActionResult NewArticle()
        {
			IEnumerable<ArticleType> listAT = vk.ArticleType;
			ViewBag.ArticleTypes = listAT;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult NewArticle(Article article)
        {
            if (ModelState.IsValid)
            {
                article.wfTitle = article.Title.Replace(' ', '_');
                if (article.Type.ToString() == null)
                    article.Type = 0;
                article.DateAdded = DateTime.Now;

                vk.Article.AddObject(article);
                vk.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Error = "Article is not valid";
                return View(article);
            }
        }

        public ActionResult ArticleDelete(int id)
        {
            var ArticleForDelete = vk.Article.Where(a => a.id == id).First();
            vk.DeleteObject(ArticleForDelete);
            vk.SaveChanges();
            return RedirectToAction("ArticleList");
        }

        public ActionResult SkillList()
        {
            var skills = vk.Skills.OrderBy(s => s.Order);
            return View(skills.ToList());
        }

        public ActionResult SkillEdit(int id)
        {
            var skill = vk.Skills.Where(s => s.id == id).FirstOrDefault();
            return View(skill);
        }

        [HttpPost]
        public ActionResult SkillEdit(Skills skill)
        {
            if (ModelState.IsValid)
            {
                vk.Skills.Attach(skill);
                vk.ObjectStateManager.ChangeObjectState(skill, System.Data.EntityState.Modified);
                vk.SaveChanges();
                return RedirectToAction("SkillList");
            }
            else
            {
                return View(skill);
            }
        }

        public ActionResult SkillNew()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SkillNew(Skills skill)
        {

            if (ModelState.IsValid)
            {
                vk.AddToSkills(skill);
                vk.SaveChanges();
                return RedirectToAction("SkillList");
            }
            else
            {
                return View(skill);
            }
        }

        public ActionResult SkillDelete(int id)
        {
            var SkillForDelete = vk.Skills.Where(s => s.id == id).First();
            vk.DeleteObject(SkillForDelete);
            vk.SaveChanges();
            return RedirectToAction("SkillList");
        }
    }
}
